// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (The system cannot find the path specified))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.ba360.struts.form;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.ba360.portal.common.PortalConstants;

import com.ba360.portal.util.Log;

/**
 * NewAdRecord.java created by EasyStruts - XsltGen. http://easystruts.sf.net
 * created on 05-02-2004
 * 
 * XDoclet definition:
 * 
 * @struts:form name="NewAdRecord"
 */
public class NewAdRecord extends ActionForm {

	// --------------------------------------------------------- Instance
	// Variables

	/** contactName property */
	private String contactName;

	/** postDesc property */
	private String postDesc;

	/** title property */
	private String title;

	/** price property */
	private String price;

	/** contactNumber property */
	private String contactNumber;

	/** activeUntil property */
	private String activeUntil;

	/** contactEmail property */
	private String contactEmail;

	private String isAnonymize;

	private String category_id;

	private String sub_cat_id;

	private String city_id;

	private String catName;

	private String subCatName;

	private String city;

	private String datePosted;

	private String adId;

	private int memberId;

	// variables for uploaded files.
	private FormFile theFile1;

	private FormFile theFile2;

	private FormFile theFile3;

	private FormFile theFile4;

	private FormFile theFile5;

	private FormFile theFile6;

	public void setAdId(String adId) {
		this.adId = adId;
	}

	public String getAdId() {
		return adId;
	}

	public void setCategory_id(String category_id) {
		this.category_id = category_id;
	}

	public String getCategoryId() {
		return category_id;
	}

	public String getCategory_id() {
		return category_id;
	}

	public void setDatePosted(String datePosted) {
		this.datePosted = datePosted;
	}

	public String getDatePosted() {
		return datePosted;
	}

	public void setSubCat_id(String sub_cat_id) {
		this.sub_cat_id = sub_cat_id;
	}

	public String getSubCat_id() {
		return sub_cat_id;
	}

	public String getSub_cat_id() {
		return sub_cat_id;
	}

	public void setCity_id(String city_id) {
		this.city_id = city_id;
	}

	public String getCity_id() {
		return city_id;
	}

	public void setCatName(String catName) {
		this.catName = catName;
	}

	public String getCatName() {
		return catName;
	}

	public void setSubCatName(String subCatName) {
		this.subCatName = subCatName;
	}

	public String getSubCatName() {
		return subCatName;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCity() {
		return city;
	}

	public String getIsAnonymize() {
		Log.info("getting is anonymize: " + isAnonymize);
		if (isAnonymize == null)
			return "off";
		return isAnonymize;
	}

	public void setIsAnonymize(String isAnonymize) {
		// Log.info("Seeting is anonymize: " + isAnonymize);
		this.isAnonymize = isAnonymize;
	}

	// For Housing
	private String squareFeet;

	private String houseType;

	public void setSquareFeet(String squareFeet) {
		this.squareFeet = squareFeet;
	}

	public String getSquareFeet() {
		return squareFeet;
	}

	public void setHouseType(String houseType) {
		this.houseType = houseType;
	}

	public String getHouseType() {
		return houseType;
	}

	// --------------------------------------------------------- Methods

	/**
	 * Method validate
	 * 
	 * @param ActionMapping
	 *            mapping
	 * @param HttpServletRequest
	 *            request
	 * @return ActionErrors
	 */
	private boolean editMode = false;

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		// throw new UnsupportedOperationException("Generated method
		// 'validate(...)' not implemented.");

		Log.init(this.getClass());
		ActionErrors ae = new ActionErrors();
		String action = (String) request.getParameter(PortalConstants.ACTION);
		if (action != null) {
			if (action.equals("EditAd")) {
				// System.out.println("editMode "+editMode );
				editMode = true;
			} else if (action.equals("PublishAd")) {
				editMode = true;
			} else if (action.equals("DisplayAd")) {
				editMode = false;
			}
		}
		Log.info("Action: " + action);
		if (action == null) {

			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			cal.add(Calendar.DATE, 30);
			Date newDate = cal.getTime();
			activeUntil = new java.text.SimpleDateFormat("dd-MM-yyyy")
					.format(newDate);
			// datePosted = new java.text.SimpleDateFormat("MM-dd-yyyy").format(
			// new Date());
			DateFormat dft = DateFormat.getDateTimeInstance(DateFormat.LONG,
					DateFormat.LONG, new Locale("en", "US"));
			// datePosted = dft.format(new Date());
			// Date d = new java.text.SimpleDateFormat("dd").format();
			String postingType = request.getParameter("PostingType");
			String subCatId = request.getParameter("CatType");
			String city_id = request.getParameter("City");

			if (postingType == null) {
				Log.info("Posting type not found");
				ae.add("Main Category",
						new ActionError("error.maincat.isempty"));
				return ae;
			}
			if (subCatId == null) {
				Log.info("SubCatId not found");
				ae
						.add("Main Category", new ActionError(
								"error.subcat.isempty"));
				return ae;
			}
			if (city_id == null) {
				Log.info("CityId not found");
				ae.add("Main Category", new ActionError(
						"error.location.isempty"));
				return ae;
			}

			title = "";
			postDesc = "";
			contactEmail = "";
			contactName = "";
			contactNumber = "";
			price = "";
			squareFeet = "";
			isAnonymize = null;

			return ae;
		}

		if (action.equals("PostAd")) {
			// throw new UnsupportedOperationException("Generated method
			// 'validate(...)' not implemented.");
			String mask = request.getParameter("isAnonymize");
			if (mask == null) {
				isAnonymize = null;
			} else {
				isAnonymize = "on";
			}
			Log.info("PostAd: " + isAnonymize + "  "
					+ request.getParameter("isAnonymize"));

			if (activeUntil == null) {
				ae.add("Main Category", new ActionError("error.date.isempty"));
				return ae;

			}
			if (activeUntil != null) {
				SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

				Date setDate = null;
				try {

					setDate = formatter.parse(activeUntil);
				} catch (Exception e) {
					ae.add("Main Category", new ActionError(
							"error.dateactive.formaterror"));
					return ae;
				}
				Calendar call = Calendar.getInstance();
				// call.set
				call.setTime(new Date());
				// call.add(Calendar.DATE, 30);
				Date validDate = call.getTime();
				Log.info("Date Set: " + formatter.format(setDate)
						+ "   Valid Date: " + formatter.format(validDate));
				if (setDate.before(validDate)) {
					ae.add("Main Category", new ActionError(
							"error.dateactive.isinvalid"));
					return ae;
				}
				call.add(Calendar.DATE, 30);
				validDate = call.getTime();
				if (setDate.after(validDate)) {
					ae.add("Main Category", new ActionError(
							"error.dateactive.isinvalid"));
					return ae;
				}

			}
			if (title == null || title.trim().length() == 0) {
				ae.add("Title", new ActionError("error.title.isempty"));
				return ae;

			}

			if (catName.equals("Housing") || catName.equals("Autos")
					|| catName.equals("Buy & Sell")) {
				if (price != null && price.trim().length() > 0) {
					try {
						float priceValue = Float.parseFloat(price.substring(2));

					} catch (Exception e) {
						ae.add("Price", new ActionError("error.price.invalid"));
						return ae;
					}

				}
			}
			if (catName.equals("Housing")) {
				if (squareFeet != null && squareFeet.trim().length() > 0) {
					try {
						long sqft = Long.parseLong(squareFeet);

					} catch (Exception e) {
						ae.add("Square Feet", new ActionError(
								"error.squarefeet.invalid"));
						return ae;
					}
				}
			}
			if (postDesc == null || postDesc.trim().length() == 0) {
				ae.add("PostDesc", new ActionError("error.postDesc.isempty"));
				return ae;

			}
			if (contactEmail == null || contactEmail.trim().length() == 0) {
				ae.add("ContactEmail", new ActionError(
						"error.contactEmail.isempty"));
				return ae;

			}
			/*
			 * if (contactName == null || contactName.trim().length() == 0) {
			 * ae.add( "ContactName", new
			 * ActionError("error.contactName.isempty")); return ae; }
			 */
			/*
			 * if (contactNumber == null || contactNumber.trim().length() == 0) {
			 * ae.add( "ContactNumber", new
			 * ActionError("error.contactNumber.isempty")); return ae; }
			 */
		}

		return ae;
	}

	/**
	 * Method reset
	 * 
	 * @param ActionMapping
	 *            mapping
	 * @param HttpServletRequest
	 *            request
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		/*
		 * contactName = "";
		 * 
		 * 
		 * postDesc = "";
		 * 
		 * 
		 * title = "";
		 * 
		 * 
		 * price = "";
		 * 
		 * 
		 * contactNumber = "";
		 * 
		 * 
		 * contactEmail = ""; squareFeet = ""; houseType = "";
		 */
		// price==null;
	}

	/**
	 * Returns the contactName.
	 * 
	 * @return String
	 */
	public String getContactName() {
		return contactName;
	}

	/**
	 * Set the contactName.
	 * 
	 * @param contactName
	 *            The contactName to set
	 */
	public void setContactName(String contactName) {
		this.contactName = contactName;
	}

	/**
	 * Returns the postDesc.
	 * 
	 * @return String
	 */
	public String getPostDesc() {
		// System.out.println("Get post desc editMode "+editMode );
		if (editMode) {
			return postDesc;
		} else {

			String pD = postDesc.replaceAll("\n", "<br>");
			Log.info(pD);
			return pD;
		}// Formatter.addNewLine(postDesc,75);
	}

	/**
	 * Set the postDesc.
	 * 
	 * @param postDesc
	 *            The postDesc to set
	 */
	public void setPostDesc(String postDesc) {
		this.postDesc = postDesc;
	}

	/**
	 * Returns the title.
	 * 
	 * @return String
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Set the title.
	 * 
	 * @param title
	 *            The title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * Returns the price.
	 * 
	 * @return String
	 */
	public String getPrice() {
		if (price.indexOf("Rs") != -1)
			return price;
		return "Rs" + price;
	}

	/**
	 * Set the price.
	 * 
	 * @param price
	 *            The price to set
	 */
	public void setPrice(String price) {
		this.price = price;
	}

	/**
	 * Returns the contactNumber.
	 * 
	 * @return String
	 */
	public String getContactNumber() {
		return contactNumber;
	}

	/**
	 * Set the contactNumber.
	 * 
	 * @param contactNumber
	 *            The contactNumber to set
	 */
	public void setContactNumber(String contactNumber) {
		this.contactNumber = contactNumber;
	}

	/**
	 * Returns the activeUntil.
	 * 
	 * @return String
	 */
	public String getActiveUntil() {
		return activeUntil;
	}

	/**
	 * Set the activeUntil.
	 * 
	 * @param activeUntil
	 *            The activeUntil to set
	 */
	public void setActiveUntil(String activeUntil) {
		this.activeUntil = activeUntil;
	}

	/**
	 * Returns the contactEmail.
	 * 
	 * @return String
	 */
	public String getContactEmail() {
		return contactEmail;
	}

	/**
	 * Set the contactEmail.
	 * 
	 * @param contactEmail
	 *            The contactEmail to set
	 */
	public void setContactEmail(String contactEmail) {
		this.contactEmail = contactEmail;
	}

	public int getMemberId() {
		return memberId;
	}

	public void setMemberId(int memberId) {
		this.memberId = memberId;
	}

	/**
	 * @return Returns the theFile1.
	 */
	public FormFile getTheFile1() {
		return theFile1;
	}

	/**
	 * @param theFile1
	 *            The theFile1 to set.
	 */
	public void setTheFile1(FormFile theFile1) {
		this.theFile1 = theFile1;
	}

	/**
	 * @return Returns the theFile2.
	 */
	public FormFile getTheFile2() {
		return theFile2;
	}

	/**
	 * @param theFile2
	 *            The theFile2 to set.
	 */
	public void setTheFile2(FormFile theFile2) {
		this.theFile2 = theFile2;
	}

	/**
	 * @return Returns the theFile3.
	 */
	public FormFile getTheFile3() {
		return theFile3;
	}

	/**
	 * @param theFile3
	 *            The theFile3 to set.
	 */
	public void setTheFile3(FormFile theFile3) {
		this.theFile3 = theFile3;
	}

	/**
	 * @return Returns the theFile4.
	 */
	public FormFile getTheFile4() {
		return theFile4;
	}

	/**
	 * @param theFile4
	 *            The theFile4 to set.
	 */
	public void setTheFile4(FormFile theFile4) {
		this.theFile4 = theFile4;
	}

	/**
	 * @return Returns the theFile5.
	 */
	public FormFile getTheFile5() {
		return theFile5;
	}

	/**
	 * @param theFile5
	 *            The theFile5 to set.
	 */
	public void setTheFile5(FormFile theFile5) {
		this.theFile5 = theFile5;
	}

	/**
	 * @return Returns the theFile6.
	 */
	public FormFile getTheFile6() {
		return theFile6;
	}

	/**
	 * @param theFile6
	 *            The theFile6 to set.
	 */
	public void setTheFile6(FormFile theFile6) {
		this.theFile6 = theFile6;
	}
}